Analysis 2 of global and $GLOBALS[] in the PHP language

  • 2020-05-12 02:23:00
  • OfStack

Let's borrow the example from the last one:

PHP code
 
<?php 
//  example 1 
function test_global() { 
global $var1, $var2; 
$var2 =& $var1; 
} 
function test_globals() { 
$GLOBALS['var3'] =& $GLOBALS['var1']; 
} 
$var1 = 5; 
$var2 = $var3 = 0; 
test_global(); 
print $var2 . " \n " ; 
test_globals(); 
print $var3 . " \n " ; 
?> 

The execution results are as follows:
0
5
How could that be? Shouldn't it be two fives? How do you get a 0 and a 5?

Well, let's keep the above questions in mind and delve into the principles of $GLOBALS and global!
We all know that a variable is actually a "code name" in the corresponding physical code
Explanation of $GLOBALS cited in the php manual:
The Global variable: $GLOBALS, note: $GLOBALS is available in PHP 3.0.0 and later.
An array of all defined global variables. The variable name is the index of the array. This is an "superglobal" or can be described as an automatic global variable.
That is, $var1 and $GLOBALS['var1'] in the code above refer to the same variable, not two different variables!
So what does global do?
Explanation of global using the php manual:
If you assign a reference to a variable declared as global within a function, the reference is visible only within the function. You can avoid this point by using the $GLOBALS array.
We all know that the variables generated by functions in php are private variables of functions, so the variables generated by the global keyword cannot escape this rule. Why do you say this?
PHP code
 
<?php 
//  example 2 
function test() { 
global $a; 
unset($a); 
} 
$a = 1; 
test(); 
print $a; 
?> 

The execution results are as follows:
1
Why would it print 1? Isn't $a already given to unset? unset doesn't work? php bug?
No, actually, unset works. It just dropped $a from the test function to unset. You can add it to the test() function
print $a;
To test!
Going back to example 1 above, look at the 1 code in test_global "$var2 =& $var1;" , above is a reference assignment operation, that is, $var2 will point to the physical memory address pointed to by var1. Therefore, after example 1 executes the test_global function, the change of the variable will only have an effect on the local part of the function. Outside the function, the physical memory address pointed to by $var2 does not change, but remains the same.
At this point, you can understand why $var2 is 0 and $var3 is 5 after example 1 has been executed!
Therefore, we draw a conclusion that the difference between global and $GLOBALS[] in the function is as follows:
global generates an alias variable that points to the external variable of the function, not the actual external variable of the function, 1 but changes the address to which the alias variable points, something unexpected happens, such as example 1.
The $GLOBALS[] call is really an external variable, and the inside and outside of the function will always be 1 to 1
The following two can be compared to deepen the impression:
global:
 
<?php 
function myfunction(){ 
global $bar; 
unset($bar); 
} 
$bar= " someting " ; 
myfunction(); 
echo $bar; 
?> 

Output: someting
$GLOBALS[]:
 
<?php 
function foo() 
{ 
unset($GLOBALS['bar']); 
} 
$bar =  " something " ; 
foo(); 
echo $bar; 
?> 

Output: empty
After understanding according to the train of thought above, the circumstance that encounters below is a little dizzy again?
 
<?php 
$a = 1; 
$b = 2; 
function Sum() 
{ 
global $a, $b; 
$b = $a + $b; 
} 
Sum(); 
echo $b; 
?> 

The output will be "3". Global variables $a and $b are declared in the function, and all references to any variable point to the global variable.
Why is it not 2? It doesn't matter outside the function. Notice that $b is not modified by reference in the function. Instead, the modified $b points to the value of physical memory, so the external input is 3.

Related articles: